package chaowan.Mapper;

import chaowan.pojo.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsMapper {

    @Select("SELECT * FROM goods")
    List<Goods> selectAll();

    @Select("SELECT * FROM goods WHERE gid = #{gid}")
    Goods selectById(Long gid);

    @Insert("INSERT INTO goods(name, intro, price, stock, cover_url, category_id, status, add_time) " +
            "VALUES(#{name}, #{intro}, #{price}, #{stock}, #{coverUrl}, #{categoryId}, #{status}, #{addTime})")
    @Options(useGeneratedKeys = true, keyProperty = "gid")
    void insert(Goods goods);

    @Update("UPDATE goods SET name=#{name}, intro=#{intro}, price=#{price}, stock=#{stock}, " +
            "cover_url=#{coverUrl}, category_id=#{categoryId}, status=#{status}, add_time=#{addTime} " +
            "WHERE gid=#{gid}")
    void update(Goods goods);

    @Delete("DELETE FROM goods WHERE gid ={gid}")
    void deleteById(Long gid);

    @Select("SELECT * FROM goods WHERE name LIKE CONCAT('%', #{keyword}, '%')")
    List<Goods> searchByKeyword(String keyword);

    @Select("SELECT * FROM goods WHERE category_id = #{cid}")
    List<Goods> findByCategoryId(Long cid);
}